package com.coocaa.ops.admin.v1.auth.sys.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.coocaa.ops.admin.v1.auth.sys.biz.ISysRunBiz;
import com.coocaa.ops.admin.v1.auth.sys.mapper.ISysRunMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.biz.auth.entity.SysRunEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/3/11.
 * @description
 */
@Service
public class SysRunBizImpl extends BaseBizImpl<ISysRunMapper, SysRunEntity> implements ISysRunBiz {
    /**
     * 获取菜单运行权限列表
     *
     * @param menuId
     * @param roles
     * @param tools  true-权限工具栏
     * @param shownOnList 在列表页面显示的
     * @param shownOnPreview 在预览页面显示的
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<SysRunEntity> listMenuRunsByMenuIdAndRoles(Integer menuId, List<Integer> roles, Boolean tools, Boolean shownOnList, Boolean shownOnPreview) {
        List<SysRunEntity> sysRuns = new ArrayList<>();
        //如果角色是空，不获取任何菜单
        if (CollectionUtils.isNotEmpty(roles)) {
            Map<String, Object> map = new HashMap<>(3);
            map.put("menuId", menuId);
            map.put("roles", roles);
            map.put("tools", tools);
            if (shownOnList != null) {
                map.put("shownOnList", shownOnList);
            }
            if (shownOnPreview != null) {
                map.put("shownOnPreview", shownOnPreview);
            }
            sysRuns = getMapper().listMenuRuns(map);
        }
        return sysRuns;
    }

    /**
     * 获取菜单运行权限列表
     *
     * @param menuId
     * @param tools  true-权限为工具栏
     * @param shownOnList 在列表页面显示的
     * @param shownOnPreview 在预览页面显示的
     * @return
     */
    @Override
    public List<SysRunEntity> listMenuRuns(Integer menuId, Boolean tools, Boolean shownOnList, Boolean shownOnPreview) {
        //与上一个方法的区别是不查询角色，
        Map<String, Object> map = new HashMap<>(2);
        map.put("menuId", menuId);
        map.put("tools", tools);
        if (shownOnList != null) {
            map.put("shownOnList", shownOnList);
        }
        if (shownOnPreview != null) {
            map.put("shownOnPreview", shownOnPreview);
        }
        return getMapper().listMenuRuns(map);
    }

    /**
     * 获取权限按钮-通过英文名字
     *
     * @param runComm
     * @return
     */
    @Override
    public SysRunEntity getSysRunByRunComm(String runComm) {
        QueryWrapper<SysRunEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("run_comm", runComm);
        List<SysRunEntity> list = getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }
}
